package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appRoleDao")
public class AppRoleDao {

	public List<AppRole> queryByAppID(String appId) throws SQLException {
		List<AppRole> arList = new ArrayList<AppRole>();
		AppRole dc = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM app_role WHERE app_id=?");
			ps.setString(1, appId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new AppRole();
				dc.setAppId(rs.getString("app_id"));
				dc.setAppRoleId(rs.getString("app_role_id"));
				dc.setAppRoleName(rs.getString("app_role_name"));
				arList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryAppTradePrivateWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return arList;
	}

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppRole queryWithKeys(String appId, String appRoleId)
			throws SQLException {
		AppRole dc = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM app_role WHERE app_id=? and app_role_id=?");
			ps.setString(1, appId);
			ps.setString(2, appRoleId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new AppRole();
				dc.setAppId(rs.getString("app_id"));
				dc.setAppRoleId(rs.getString("app_role_id"));
				dc.setAppRoleName(rs.getString("app_role_name"));
				dc.setRemarks(rs.getString("remarks"));

			}
		} catch (SQLException e) {
			throw new SQLException("queryAppTradePrivateWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return dc;
	}

	public int saveAppRole(AppRole appRole) throws SQLException {

		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into app_role values(?,?,?,?);";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appRole.getAppId());
			ps.setString(2, appRole.getAppRoleId());
			ps.setString(3, appRole.getAppRoleName());
			ps.setString(4, appRole.getRemarks());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	/**
	 * 修改模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int update(AppRole appRole) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update  app_role  set APP_ROLE_NAME=?,REMARKS=? where app_id=? and APP_ROLE_ID=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appRole.getAppRoleName());
			ps.setString(2, appRole.getRemarks());
			ps.setString(3, appRole.getAppId());
			ps.setString(4, appRole.getAppRoleId());

			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;

	}

	public int delete(String appId, String appRoleId) throws SQLException {
		// TODO Auto-generated method stub
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from  app_role  where app_id=? and APP_ROLE_ID=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appId);
			ps.setString(2, appRoleId);

			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("queryAppFrameStartWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
